import alert from './uni_modules/uview-plus/components/u-alert/alert';
import helper from '@/common/helper.js'
export default {
	doRequest(method, url, data, contentType, type = true) {
		var apiurl = helper.data().serverUrl;
		//apiurl=process.env.VUE_APP_USER;
		// console.log(process.env);
		var token = uni.getStorageSync('ctoken');
		//不需要验证token的接口
		if (type == true) {
			uni.showLoading()
		}
		// console.log(token);
		return new Promise((resolve, reject) => {
			let header = {}
			if (contentType === 'nojson') {
				// console.log(2);
				header = {
					'Accept': 'application/vnd..v1+json',
					'content-type': 'application/x-www-form-urlencoded',
					'jwttoken': token,
				}
			} else {
				// console.log(1);
				header = {
					'Accept': 'application/vnd..v1+json',
					'content-type': 'application/json',
					'jwttoken': token,
				}
			}
			uni.request({
				method: method,
				url: apiurl + url,
				data: data,
				// data:data
				header: header,
				success: function(result) {
					if (result.statusCode == 200) {
						if(typeof result.data ==='string' ){
							
							result.data=JSON.parse(result.data);
							
						}
						if(result.data.code==888){
							uni.showToast({
								title:result.data.msg,
								icon:"none"
							});
							setTimeout(function(){
								uni.reLaunch({
									url:"/pages/login/login"
								})
							},1000);
						}
					} else {
						if (result.statusCode != '438' && result.statusCode != '435'  && result
							.statusCode != '422' && result.statusCode != '415') {
							uni.showToast({
								title: result.data.msg,
								icon: 'none'
							})
						}
						if(result.data.msg=='验证码错误'){
							uni.showToast({
								title:result.data.msg,
								icon:"none"
							})
						}

					}
					uni.hideLoading();
					uni.stopPullDownRefresh()
					// resolve调用后，即可传递到调用方使用then或者async+await同步方式进行处理逻辑
					resolve(result)
				},
				fail: function(e) {
					uni.$emit('z-paging-error-emit');
					console.log('error in...')
					// reject调用后，即可传递到调用方使用catch或者async+await同步方式进行处理逻辑
					reject(e)
					// uni.showToast({
					// 	title: "网络连接失败",
					// 	duration: 3500,
					// 	icon: "error"
					// });
					
				},
			})
		})

	},

}